var path;
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        loading: false,
        shadow: true,
        really: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
        //token = decodeURIComponent(options.scene)
        console.log('options====>', options)

        const that = this;
        path = options.path;
        if (options.goodsId) {
            let goodsId = options.goodsId;
            that.setData({
                goodsId: goodsId
            })
        }

    },
    // 授权操作
    onGotUserInfo(e) {
        if (wx.getUserProfile) {
            this.setData({
                canIUseGetUserProfile: true
            })
        }
        const that = this;
        console.table(e);
        wx.getUserProfile({
            desc: '用于完善会员资料',
            lang: "zh_CN",
            success: function (resp) {
                if (resp.errMsg == 'getUserProfile:ok') {
                    //授权允许
                    that.customLogin(resp, resp.encryptedData, resp.iv);
                } else {
                    //拒绝授权
                    // 没有userId需要注册
                }

            }, fail: function (fres) {
                wx.showToast({
                    title: '感谢您的关注,欢迎下次再来 (#^.^#)',
                    icon: 'none',
                    duration:3000
                });
            }
        },)
    },

    /**
     *  登录
     */
    customLogin(u, encyData, ivData) {
        let that = this;
        let channelId = wx.getStorageSync('channelId');
        let fromUserId = wx.getStorageSync('fromUser');

        if(fromUserId !=null && fromUserId !=undefined && fromUserId !=""){
        } else {
            fromUserId = null;
        }
        wx.login({
            success: function (res) {
                if (res.code) {
                    that.setData({
                        loading: true,
                        really: false
                    })
                    wx.request({
                        url: getApp().Api_url + 'user/getOpenId2',
                        method: 'GET',
                        data: {
                            jsCode: res.code,
                            encyData: encyData,
                            ivData: ivData,
                            parent: fromUserId,
                            channelId: channelId
                        },
                        success: (response) => {
                            console.log("授权接口响应结果:", response);
                            let success = response.data.code;
                            if (success == 200) {
                                that.setData({
                                    loading: true,
                                })
                                let userId = response.data.data;
                                if (userId) {
                                    wx.setStorageSync('userId', userId);
                                    wx.setStorageSync('userInfo', u.userInfo);
                                    wx.setStorageSync('username',response.data.nickName);
                                    wx.setStorageSync('usericon', response.data.avatar);

                                    //调取检测用户父级id接口，没有的话就不是通过邀请进入的用户需要绑定管家
                                    wx.request({
                                        url: getApp().Api_url + 'user/hasFather',
                                        method: 'GET',
                                        header: {
                                            'content-type': 'application/x-www-form-urlencoded',
                                        },
                                        data: {
                                            userId: userId,
                                        },
                                        success: (res) => {
                                            console.log('调取检测是否有父级===>', res.data.errMsg)
                                            if (res.data.errMsg == 'OK') {
                                                if (res.data.data == 1) { //没有管家
                                                    console.log('没有父亲')
                                                    wx.reLaunch({
                                                        url: '/pages/qxzgj/qxzgj'
                                                    })
                                                } else {
                                                    console.log('有父亲')
                                                    if (that.data.goodsId) {
                                                        console.log('有商品id')
                                                        //有商品id
                                                        wx.reLaunch({
                                                            url: '/pages/gift_details/gift_details?goodsId=' + that.data.goodsId + '&show=0',
                                                        })
                                                    } else {
                                                        console.log('没有商品id')
                                                        console.log('path===>', path)
                                                        //没有商品id
                                                        wx.switchTab({
                                                            url: path
                                                        })
                                                    }
                                                }

                                            }
                                        }
                                    })
                                } else {
                                    console.log(' 以前注册过有userid')
                                    //有userId存起来
                                    wx.setStorageSync('userId', response.data.data);
                                    wx.setStorageSync('userInfo', u.userInfo);
                                    console.log('goodsId===>', that.data.goodsId)
                                    if (that.data.goodsId) { //有商品id
                                        console.log('有商品id')
                                        wx.reLaunch({
                                            url: '/pages/gift_details/gift_details?goodsId=' + that.data.goodsId + '&show=0',
                                        })
                                    } else { //没有商品id
                                        console.log('没有商品id')
                                        console.log('path===>', path)
                                        wx.switchTab({
                                            url: path
                                        })
                                    }
                                }

                            } else {
                                wx.showToast({
                                    icon: 'none',
                                    title: '处理中...',
                                })
                            }
                        }
                    })
                }
            }
        })
    },

    onBack() {
        const that = this;

        wx.switchTab({
            url: '/pages/list/list',
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log('onshow==>>>')

        const that = this;
        if (!wx.getStorageSync('userId')) {
            console.log('没有userid login')
        } else {
            console.log('有userid')
            console.log('goodsId===>', that.data.goodsId)
            if (that.data.goodsId) {
                wx.navigateTo({
                    url: '/pages/gift_details/gift_details?goodsId=' + that.data.goodsId + '&show=0',
                })
            } else {
                wx.switchTab({
                    url: '/pages/list/list',
                })
            }
        }


    },

})